package com.genesis.yunnanji.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.genesis.yunnanji.R;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;


public class MapPoi extends BaseActivity {
    private AMap mAmap;
    private MapView mMapView;
    private Double lat;
    private Double lng;
    private PoiSearch.Query query;
    private List<Marker> markers = new ArrayList<>();
    private TabLayout mTabLayout;
    private String[] poiName;
    private int[] tabBacks = new int[]{R.drawable.school, R.drawable.hospital, R.drawable.bank,
            R.drawable.metro, R.drawable.transit, R.drawable.shopping, R.drawable.restaurant, R.drawable.park};
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_poi);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 2);
        mToolbar = (Toolbar) findViewById(R.id.tb_poi_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        poiName = getResources().getStringArray(R.array.poi_name);
        lat = getIntent().getDoubleExtra("lat", 0);
        lng = getIntent().getDoubleExtra("lng", 0);
        mTabLayout = (TabLayout) findViewById(R.id.bottom_poi_bar);
        initTab();
        mMapView = (MapView) findViewById(R.id.mapview_poi_map);
        mMapView.onCreate(savedInstanceState);
        if (mAmap == null) {
            mAmap = mMapView.getMap();
        }
        searchPoi("学校");


    }

    @Override
    protected void setUpView() {
        LatLng latLng = new LatLng(lat, lng);
        mAmap.addMarker(new MarkerOptions().position(latLng).draggable(false));
        mAmap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
    }


    public void searchPoi(String keywords) {
        PoiSearch.Query query = new PoiSearch.Query(keywords, "", "");
        query.setPageSize(10);
        query.setPageNum(1);
        PoiSearch poiSearch = new PoiSearch(context, query);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(lat, lng), 2000));
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                for (int j = 0; j < poiResult.getPois().size(); j++) {
                    Marker marker = mAmap.addMarker(new MarkerOptions().position(new LatLng(poiResult.getPois().get(j).getLatLonPoint().getLatitude(), poiResult.getPois().get(j).getLatLonPoint().getLongitude()))
                            .draggable(false)
                            .title(poiResult.getPois().get(j).getTitle()).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                                    .decodeResource(getResources(), R.mipmap.spot))));
                    markers.add(marker);
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
        poiSearch.searchPOIAsyn();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }


    public void initTab() {
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        for (int i = 0; i < 8; i++) {
            TabLayout.Tab tab = mTabLayout.newTab();
            tab.setCustomView(getTabView(i));
            mTabLayout.addTab(tab);
        }
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (markers != null) {
                    for (Marker marker : markers) {
                        marker.remove();
                    }
                }
                searchPoi(poiName[tab.getPosition()]);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_item_tab, null);
        TextView txt_title = (TextView) view.findViewById(R.id.tv_tabitem_text);
        txt_title.setText(poiName[position]);
        ImageView img_title = (ImageView) view.findViewById(R.id.iv_tabitem_icon);
        img_title.setBackgroundResource(tabBacks[position]);
        return view;
    }
}
